/****************************************************************************
 * Elisa 1.0                                                                *
 *--------------------------------------------------------------------------*
 * Author: Laurent Granvilliers, Vincent Sorin                              *
 * Copyright (c) 2004 Laboratoire d'Informatique de Nantes Atlantique       *
 *--------------------------------------------------------------------------*
 * Elisa is distributed WITHOUT ANY WARRANTY. Read the associated           *
 * COPYRIGHT file for more details.                                         *
 *--------------------------------------------------------------------------*
 * elisa_constraint.cpp                                                     *
 ****************************************************************************/

#include "elisa_constraint.h"

USING_ELISA

//--------------------------------------------------------------------------
// class ConstraintRep
//--------------------------------------------------------------------------

void ConstraintRep::addExpr(SymbolicExpression *e)
{
	if (e != 0) {
		m_exprs.push_back(SymbolicExpressionPtr(e));
	}
}

int ConstraintRep::arity() const
{
	return m_exprs.size();
}

ConstraintRep::~ConstraintRep()
{
}

void ConstraintRep::toVariables(VarImplSet & s) const
{
	for (unsigned int i = 0; i < m_exprs.size(); ++i) {
		m_exprs[i]->toVariables(s);
	}
}

void ConstraintRep::initVariableImplementation(SearchTree & searchTree, VarImplSet & s)
{
	for (unsigned int i = 0; i < m_exprs.size(); ++i) {
		m_exprs[i]->initVariableImplementation(searchTree);
		m_exprs[i]->toVariables(s);
	}
}

int ConstraintRep::multiplicity(VariableImpl & v) const
{
	int sum = 0;
	for (unsigned int i = 0; i < m_exprs.size(); ++i) {
		sum += m_exprs[i]->multiplicity(v);
	}
	return sum;
}

int ConstraintRep::addRef()
{
	return ++m_nref;
}

int ConstraintRep::delRef()
{
	if (--m_nref == 0) {
		delete this;
	}
	return m_nref;
}

int ConstraintRep::ref() const
{
	return m_nref;
}

//--------------------------------------------------------------------------
// class Constraint
//--------------------------------------------------------------------------

void Constraint::toVariables(VarImplSet & v)
{
	v.insert(m_vars.begin(), m_vars.end());
}

void Constraint::initVariableImplementation(SearchTree & searchTree)
{
	if (m_rep != 0) {
		m_rep->initVariableImplementation(searchTree, m_vars);
	}
}

int Constraint::multiplicity(VariableImpl & v) const
{
	if (m_rep != 0) {
		return m_rep->multiplicity(v);
	} else {
		return 0;
	}
}

void Constraint::print(std::ostream & os) const
{
	if (m_rep != 0) {
		m_rep->print(os);
	}
}
